home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Codigo / Control del ratón / ScribbleWithSave / ScribbleWithSave.cs next >
Encoding:
Text File  |  2002-04-18  |  1.9 KB  |  66 lines

  1. //-----------------------------------------------
  2. // ScribbleWithSave.cs ⌐ 2001 by Charles Petzold
  3. //-----------------------------------------------
  4. using System;
  5. using System.Collections;          // Para ArrayList
  6. using System.Drawing;
  7. using System.Windows.Forms;
  8.  
  9. class ScribbleWithSave: Form
  10. {
  11.      ArrayList arrlstApts = new ArrayList();
  12.      ArrayList arrlstPts;
  13.      bool      bTracking;
  14.  
  15.      public static void Main()
  16.      {
  17.           Application.Run(new ScribbleWithSave());
  18.      }
  19.      public ScribbleWithSave()
  20.      {
  21.           Text = "Forma libre persistente";
  22.           BackColor = SystemColors.Window;
  23.           ForeColor = SystemColors.WindowText;
  24.      }
  25.      protected override void OnMouseDown(MouseEventArgs mea)
  26.      {
  27.           if (mea.Button != MouseButtons.Left)
  28.                return;
  29.  
  30.           arrlstPts = new ArrayList();
  31.           arrlstPts.Add(new Point(mea.X, mea.Y));
  32.  
  33.           bTracking = true;
  34.      }
  35.      protected override void OnMouseMove(MouseEventArgs mea)
  36.      {
  37.           if (!bTracking)
  38.                return;
  39.  
  40.           arrlstPts.Add(new Point(mea.X, mea.Y));
  41.  
  42.           Graphics grfx = CreateGraphics();
  43.           grfx.DrawLine(new Pen(ForeColor),
  44.                         (Point) arrlstPts[arrlstPts.Count - 2],
  45.                         (Point) arrlstPts[arrlstPts.Count - 1]);
  46.           grfx.Dispose();
  47.      }
  48.      protected override void OnMouseUp(MouseEventArgs mea)
  49.      {
  50.           if (!bTracking)
  51.                return;
  52.  
  53.           Point[] apt = (Point[]) arrlstPts.ToArray(typeof(Point));
  54.           arrlstApts.Add(apt);
  55.           bTracking = false;
  56.      }
  57.      protected override void OnPaint(PaintEventArgs pea)
  58.      {
  59.           Graphics grfx = pea.Graphics;
  60.           Pen      pen  = new Pen(ForeColor);
  61.  
  62.           for (int i = 0; i < arrlstApts.Count; i++)
  63.                grfx.DrawLines(pen, (Point[]) arrlstApts[i]);
  64.      }
  65. }
  66.